<!-- 承运商坦克罐列表 -->
<template>
  <el-dialog
    title="坦克罐列表"
    :visible.sync="dialogVisible"
    append-to-body
    width="60%"
  >
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
    >
      <template slot="menuLeft">
        <el-button
          type="primary"
          icon="el-icon-add"
          size="small"
          @click="addBinding"
          >添加绑定</el-button
        >
      </template>
      <template slot-scope="{ row, index }" slot="menu">
        <el-button
          type="primary"
          size="mini"
          @click.stop="rowUnbound(row, index)"
          >解绑</el-button
        >
      </template>
    </avue-crud>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">关 闭</el-button>
    </span>
    <selectTank ref="selectTank" @refresh="refresh()" />
  </el-dialog>
</template>

<script>
import { getTankList, bindOrUnBind } from "@/api/basedata/carriers";
import selectTank from "./selectTank";

export default {
  data() {
    return {
      dialogVisible: false,
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        height: "auto",
        calcHeight: 200,
        tip: false,
        searchShow: false,
        searchMenuSpan: 6,
        border: true,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        labelWidth: "180",
        column: [
          {
            label: "产品",
            prop: "product",
            search: true,
          },
          {
            label: "罐号",
            prop: "tankNumber",
            search: true,
          },
          {
            label: "容积",
            prop: "volume",
          },
        ],
      },
      data: [],
      carrierId: "",
    };
  },
  computed: {},
  components: {
    selectTank,
  },
  methods: {
    searchReset() {
      this.query = {};
      this.getList(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.getList(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.getList(this.page);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList(this.page);
    },
    refreshChange() {
      this.getList(this.page, this.query);
    },
    open(id) {
      this.dialogVisible = true;
      this.carrierId = id;
      this.getList(this.page);
    },
    //选择返回刷新
    refresh() {
      this.getList(this.page);
    },
    getList(page, params = {}) {
      let query = Object.assign(params, this.query);
      query = {
        ...query,
        carrierId: this.carrierId,
      };
      this.loading = true;
      getTankList(page.currentPage, page.pageSize, query).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.$nextTick(() => {
          this.$refs.crud.$refs.table.doLayout();
        });
      });
    },
    //解绑
    rowUnbound(row) {
      this.$confirm("是否确定解绑该坦克罐?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          bindOrUnBind({
            carrierId: this.carrierId,
            type: "tank",
            bindType: "unbind",
            bindId: [row.id],
          }).then(
            () => {
              this.getList(this.page);
              this.$message({
                type: "success",
                message: "操作成功!",
              });
            },
            (error) => {
              window.console.log(error);
            }
          );
        })
        .catch(() => {});
    },
    //打开选择坦克罐弹框
    addBinding() {
      this.$refs.selectTank.open(this.carrierId);
    },
  },
};
</script>

<style></style>
